import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import {config} from '../../shared/smartadmin.config';

declare var $: any;
@Component({
  	selector: 'sa-countDatabank',
  	templateUrl: './countDatabank.component.html',
})
export class CountDatabankComponent implements OnInit {
	tableData:any[] = []; //table数据
	webTableData = []; //前端做分页table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	deviceId:string = '';//设备ID
	startTime:string = '';//开始时间
	endTime:string = '';//结束时间
	
	constructor(private jsonApiService: JsonApiService) {}

	ngOnInit() {
		this.initDate().then(()=>this.load());
	}

	//初始化时间
	initDate(){
		var that = this;
		return new Promise(function (resolve, reject) {
			System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(
				()=>{
					System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{
						$("#datetimepicker1").datetimepicker({//时间插件
							format: "yyyy-mm-dd hh:ii:ss",
						    language: "zh-CN",
						    autoclose: true,
//						    minView : 2
						}).on("changeDate", function(e){
							var date = e.date;
							var time = common.getTimeMmss(date);
							$("#datetimepicker2").datetimepicker("setStartDate", time);
						});
						$("#datetimepicker2").datetimepicker({//时间插件
							format: "yyyy-mm-dd hh:ii:ss",
						    language: "zh-CN",
						    autoclose: true,
//						    minView : 2
						}).on("changeDate", function(e){
							var date = e.date;
							var time = common.getTimeMmss(date);
							$("#datetimepicker1").datetimepicker("setEndDate", time);
						});
						var date = new Date();
						$("#datetimepicker2").val(common.getTimeMmss(date));//当前时间
						date.setDate(date.getDate() - 7);//设置前一天
						$("#datetimepicker1").val(common.getTimeMmss(date));
						resolve();
					});
				}
			);
		})
	}
	//刷新
	refresh(){
		this.load();
	}

	//查询
	search_data(){
		this.curPage = 1;
		this.load();
	}
	//初始化加载list数据
	load() {
		this.startTime =  $('#datetimepicker1').val();
		this.endTime =  $('#datetimepicker2').val();
		if(this.startTime==''||this.endTime==''){
			this.jsonApiService.message("起始结束时间不能为空");
			return;
		}
		let timeCheck=common.correctTimeParams(this.startTime,this.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let reqdata = 'gridPager={"pageSize":' + this.pageSize + ',"nowPage":' + this.curPage + ',"parameters":{"startTime":"'+this.startTime+'","endTime":"'+this.endTime+'"}}'
		this.jsonApiService.http("countDocument/countDocumentforpage.html", reqdata).subscribe(result => {
			this.tableData = result.data;
			this.totalCount=result.data.length;
			this.client_pages(0,this.pageSize);
		})
	}
	del_date(){
		this.startTime = '';
		this.endTime ='';
		$("#datetimepicker1").val('').change();
		$("#datetimepicker2").val('').change();
		$("#datetimepicker2").datetimepicker("setStartDate", null);
		$("#datetimepicker1").datetimepicker("setEndDate", null);
	}
	client_pages(start,length){
		var len = start + Number(length);
		this.webTableData = this.tableData.slice(start,len);
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.client_pages(e.first,e.rows);
	}
}
